﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PatternComposite
{
    class Program
    {
        static void Main(string[] args)
        {
            Composite china = new Composite();
            china.Add(new City("shangahi",2400));
            china.Add(new City("bejing",3000));

            Composite shanxi = new Composite();
            shanxi.Add(new City("datong",2000));
            shanxi.Add(new City("yangquan", 1100));
            
            china.Add(shanxi);
            
            Console.WriteLine(china.Count());
            Console.ReadKey(); 
        }
    }


    interface ICount
    {
        int Count();
    }



   /// <summary>
   /// 无子节点的叶子
   /// </summary>
    class City : ICount
    {
        string name;
        int sum = 0;
        public City(string  _name, int _sum)
        {
            name = _name;
            sum = _sum;
        }

        public int Count()
        {
           return sum;
        }
    }


    /// <summary>
    ///  有子节点的节点
    /// </summary>
    class Composite : ICount
    {
        List<ICount> cities = new List<ICount>();
        public int Count()
        {
            int sum = 0;

            foreach (ICount city in cities)
            {
                sum += city.Count();
            }
            return sum;
        }
         
        //-------------------
        public void Add(ICount city)
        {
            cities.Add(city);
        }
        public void Delete(ICount counter)
        {
            cities.Remove(counter);
        } 
        public List<ICount> GetChild()
        {
            return cities;
        }
      
    }
}
